package net.scrumtool.controller;

import net.scrumtool.model.Collaborator;
import net.scrumtool.model.Product;
import net.scrumtool.technics.DAO;
import net.scrumtool.view.Connexion;
import net.scrumtool.view.Home;
import net.scrumtool.view.OverviewProduct;
import net.scrumtool.view.Profile;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.MessageBox;

public class CoverviewProduct {

	OverviewProduct view;

	private Collaborator collaboratorActif;

	public CoverviewProduct(OverviewProduct view) {
		this.view = view;
	}

	public void init(Collaborator collaborator, Product productActif) {

		CoverviewProductBacklog copb = new CoverviewProductBacklog(this.view);
		copb.listener(collaboratorActif, productActif);
		view.getGrpBacklog().setText("Overview - " + productActif.getName());

		// Product Name
		view.getGrpOverviewProduct().setText("Overview - " + productActif.getName());

		// Summary
		view.getLblSummaryOverviewProduct().setText(DAO.getNbSprint(productActif) + "");
		view.getLblTotalPointSprint().setText(DAO.getNbPoints(productActif) + "");

		// info users
		view.getLblInfoUser().setText("Hello " + collaborator.getFirstname() + " " + collaborator.getLastname() + "");


	}
	
	
	
	public void reload(Collaborator collaborator, Product productActif) {
		// TableViewerToDo
				view.getTableViewerToDoOverview().setContentProvider(new ArrayContentProvider());
				view.getTableViewerToDoOverview().setLabelProvider(new LabelProvider());
				view.getTableViewerToDoOverview().setInput(DAO.getUserstoryToDo(productActif));

				// TableViewerDone
				view.getTableViewerDoneOverview().setContentProvider(new ArrayContentProvider());
				view.getTableViewerDoneOverview().setLabelProvider(new LabelProvider());
				view.getTableViewerDoneOverview().setInput(DAO.getUserstoryDone(productActif));

				// TableViewerInProgress
				view.getTableViewerInProgressOverview().setContentProvider(new ArrayContentProvider());
				view.getTableViewerInProgressOverview().setLabelProvider(new LabelProvider());
				view.getTableViewerInProgressOverview().setInput(DAO.getUserstoryInProgress(productActif));

				// TableViewerRecentSprint
				view.getTableViewerRecentSprintOverviewProduct().setContentProvider(new ArrayContentProvider());
				view.getTableViewerRecentSprintOverviewProduct().setLabelProvider(new LabelProvider());
				view.getTableViewerRecentSprintOverviewProduct().setInput(DAO.getSprintsRecent(productActif));

	}

	public void listener(final Collaborator collaborator, final Product productActif) {

		view.getTabFolderOverviewProduct().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				
					reload(collaborator, productActif);
				

			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		// RETOUR VER HOME
		view.getBackOverviewProduct().addMouseListener(new MouseListener() {

			@Override
			public void mouseDoubleClick(
					org.eclipse.swt.events.MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseDown(org.eclipse.swt.events.MouseEvent arg0) {
				view.close();
				Home home = new Home();
				home.init();
				MainController mc = new MainController(home);
				mc.init(home);
				home.open();

			}

			@Override
			public void mouseUp(org.eclipse.swt.events.MouseEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		// PROFILE
		view.getLinkProfile().addMouseListener(new MouseListener() {

			@Override
			public void mouseDoubleClick(
					org.eclipse.swt.events.MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseDown(org.eclipse.swt.events.MouseEvent arg0) {
				Profile profile = new Profile();
				profile.init();
				Cprofile cProfile = new Cprofile(profile);
				cProfile.listener(collaborator, view.getLblInfoUser());
				cProfile.init(collaborator);
				profile.open();

			}

			@Override
			public void mouseUp(org.eclipse.swt.events.MouseEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		// LOGOUT
		view.getLinkLogout().addMouseListener(new MouseListener() {

			@Override
			public void mouseDoubleClick(
					org.eclipse.swt.events.MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseDown(org.eclipse.swt.events.MouseEvent arg0) {
				MessageBox messageBox = new MessageBox(view.getShell(), SWT.ICON_QUESTION | SWT.YES | SWT.NO);
				messageBox.setText("SCRUMTOOL");
				messageBox.setMessage("Are you sure you want to exit ?");
				int response = messageBox.open();
				if (response == SWT.YES) {
					Connexion connexion = new Connexion();
					connexion.init();
					Cconnexion cConnexion = new Cconnexion(connexion);
					cConnexion.init();
					view.close();
					connexion.open();
				}
			}

			@Override
			public void mouseUp(org.eclipse.swt.events.MouseEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		view.getBtnToDo().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				view.getTableViewerColumnStatus().getColumn().setText("To do");
				view.getTltmAddUserstory().setEnabled(true);
				view.getTltmModifieUserstory().setEnabled(false);
				view.getTltmRemoveUserStory().setEnabled(false);
				view.getBtnValider().setVisible(false);
				view.getTxtDescription().setText("");
				view.getTxtNom().setText("");
				view.getComboViewerSprints().getCombo().removeAll();
				view.getTxtPtAwarded().setText("");
				view.getTxtPriority().setText("");
				view.getTabFolderOverviewProduct().setSelection(1);
				view.getTableViewerBacklog().setContentProvider(new ArrayContentProvider());
				view.getTableViewerBacklog().setLabelProvider(new LabelProvider());
				view.getTableViewerBacklog().setInput(DAO.getUserstoryToDo(productActif));
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		view.getBtnInProgress().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				view.getTableViewerColumnStatus().getColumn().setText("In progress");
				view.getTltmAddUserstory().setEnabled(true);
				view.getTltmModifieUserstory().setEnabled(false);
				view.getTltmRemoveUserStory().setEnabled(false);
				view.getBtnValider().setVisible(false);
				view.getTxtDescription().setText("");
				view.getTxtNom().setText("");
				view.getComboViewerSprints().getCombo().removeAll();
				view.getTxtPtAwarded().setText("");
				view.getTxtPriority().setText("");
				view.getTabFolderOverviewProduct().setSelection(1);
				view.getTableViewerBacklog().setContentProvider(new ArrayContentProvider());
				view.getTableViewerBacklog().setLabelProvider(new LabelProvider());
				view.getTableViewerBacklog().setInput(DAO.getUserstoryInProgress(productActif));
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		view.getBtnDone().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				view.getTableViewerColumnStatus().getColumn().setText("Done");
				view.getTltmAddUserstory().setEnabled(true);
				view.getTltmModifieUserstory().setEnabled(false);
				view.getTltmRemoveUserStory().setEnabled(false);
				view.getBtnValider().setVisible(false);
				view.getTxtDescription().setText("");
				view.getTxtNom().setText("");
				view.getComboViewerSprints().getCombo().removeAll();
				view.getTxtPtAwarded().setText("");
				view.getTxtPriority().setText("");
				view.getTabFolderOverviewProduct().setSelection(1);
				view.getTableViewerBacklog().setContentProvider(new ArrayContentProvider());
				view.getTableViewerBacklog().setLabelProvider(new LabelProvider());
				view.getTableViewerBacklog().setInput(DAO.getUserstoryDone(productActif));
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

	}

}
